package org.xtense.rcp.editors.action;

import org.dom4j.Attribute;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.ui.actions.ActionGroup;
import org.xtense.rcp.editors.XmlEditor;
import org.xtense.rcp.editors.util.XmlEntry;
import org.xtense.rcp.util.ImagesContext;


public class RootPopupActionGroup extends ActionGroup {
	protected TreeViewer tv;
	protected XmlEditor editor;

	protected MenuManager menuManager;

	public RootPopupActionGroup(XmlEditor editor, TreeViewer tv) {
		this.editor = editor;
		this.tv = tv;
		menuManager = new MenuManager();

		menuManager.add(new AddElementAction());
		menuManager.add(new AddAttributeAction());
	}

	public void fillContextMenu() {
		Tree tree = tv.getTree();
		Menu menu = menuManager.createContextMenu(tree);

		tree.setMenu(menu);
	}

	private class AddElementAction extends Action {
		public AddElementAction() {
			setText("Add &Element");
			setImageDescriptor(ImagesContext
					.getImageDescriptor(ImagesContext.Element));
		}

		public void run() {
			XmlEntry entry = getSelected();
			NewDialog d = new NewDialog(new Shell(), NewDialog.Element);
			if (d.open() == InputDialog.OK) {
				XmlEntry newEntry = new XmlEntry(d.getName());
				newEntry.setValue(d.getValue());
				newEntry.setType(XmlEntry.Element);
				entry.addChild(newEntry);
				editor.setDirty(true);
				
				// add to the model Node
				Element user = DocumentHelper.createElement(newEntry.getName());
				user.setText(newEntry.getValue());
				Element parent = (Element) entry.getNode();
				parent.add(user);
				
				int index = parent.elements().size() - 1;
				Element added = (Element) parent.elements().get(index);
				newEntry.setNode(added);
			}
			tv.refresh();
		}

	}

	private class AddAttributeAction extends Action {
		public AddAttributeAction() {
			setText("Add &Attribute");
			setImageDescriptor(ImagesContext
					.getImageDescriptor(ImagesContext.Attribute));
		}

		public void run() {
			XmlEntry entry = getSelected();
			NewDialog d = new NewDialog(new Shell(), NewDialog.Attribute);
			if (d.open() == InputDialog.OK) {
				XmlEntry newEntry = new XmlEntry(d.getName());
				newEntry.setValue(d.getValue());
				newEntry.setType(XmlEntry.Attribute);
				entry.addChild(0, newEntry);
				editor.setDirty(true);
				
				// add to the model node
				Attribute ua = DocumentHelper.createAttribute(null, newEntry
						.getName(), newEntry.getValue());
				Element base = (Element) entry.getNode();
				base.add(ua);
				
				Attribute added = base.attribute(d.getName());
				newEntry.setNode(added);
			}
			tv.refresh();
		}
	}

	public void dispose() {
		tv.getTree().setMenu(null);
		super.dispose();
	}
	
	public XmlEntry getSelected() {
		IStructuredSelection selection = (IStructuredSelection) tv
				.getSelection();
		XmlEntry entry = (XmlEntry) selection.getFirstElement();
		return entry;
	}
}
